/*
 * Copyright (C) 2008 Alessandro Sivieri <alessandro.sivieri@chimera-bellerofonte.eu>.
 *
 *  This file is part of Daex.
 *
 *  Daex is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  Foobar is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Foobar.  If not, see <http://www.gnu.org/licenses/>.
 */
package eu.chimera_bellerofonte.tesi.ui.models;

import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.IOException;
import java.util.logging.Level;

import eu.chimera_bellerofonte.tesi.DaexLogger;
import eu.chimera_bellerofonte.tesi.datatypes.Song;


/**
 * Classe per la costruzione dell'oggetto da trasferire
 * durante le operazioni di copia/drag and drop.
 * 
 * @see eu.chimera_bellerofonte.tesi.datatypes.Song
 * @author Sivieri Alessandro
 * @author Vitucci Nicola
 * @version 12/mag/07
 */
public class SongTransferable implements Transferable
{
	/**
	 * Nome "human readable" da assegnare al DataFlavor.
	 * @see java.awt.datatransfer.DataFlavor
	 */
	public static final String FLAVORNAME="Song";
	/**
	 * Nome della classe da assegnare al DataFlavor.
	 */
	public static final String FLAVORCLASSNAME="eu.chimera_bellerofonte.tesi.datatypes.Song";
	
	private Song[] songs=null;
	private DataFlavor flavor=null;

	public static DataFlavor getDefaultFlavor()
	{
		try
		{
			return new DataFlavor(DataFlavor.javaJVMLocalObjectMimeType+";class="+FLAVORCLASSNAME);
		}
		catch (ClassNotFoundException e)
		{
			DaexLogger.getDaexLogger().severe("Impossibile creare il DataFlavor corrispondente all'oggetto Song!");
			return null;
		}
	}
	
	/**
	 * Costruttore; setta gli attributi di classe con i
	 * valori specificati.
	 * 
	 * @param songs le tracce da caricare nel transferable
	 */
	public SongTransferable(Song[] songs)
	{
		this.songs = songs;
		try
		{
			this.flavor=new DataFlavor(DataFlavor.javaJVMLocalObjectMimeType+";class="+FLAVORCLASSNAME);
		}
		catch (ClassNotFoundException e)
		{
			DaexLogger.getDaexLogger().log(Level.SEVERE, "Impossibile creare il flavor!", e);
		}
	}
	
	/**
	 * @see java.awt.datatransfer.Transferable#getTransferData(java.awt.datatransfer.DataFlavor)
	 */
	public Song[] getTransferData(DataFlavor arg0)
			throws UnsupportedFlavorException, IOException
	{
		if (!isDataFlavorSupported(arg0))
		{
            throw new UnsupportedFlavorException(arg0);
        }
        return songs;
	}

	/**
	 * @see java.awt.datatransfer.Transferable#getTransferDataFlavors()
	 */
	public DataFlavor[] getTransferDataFlavors()
	{
		try
		{
			String songType=DataFlavor.javaJVMLocalObjectMimeType+";class="+FLAVORCLASSNAME;
			DataFlavor[] flavors={new DataFlavor(songType)};
			return flavors;
		}
		catch (ClassNotFoundException e)
		{
			DaexLogger.getDaexLogger().severe("Impossibile creare il DataFlavor corrispondente all'oggetto Song!");
			return new DataFlavor[0];
		}
	}

	/**
	 * @see java.awt.datatransfer.Transferable#isDataFlavorSupported(java.awt.datatransfer.DataFlavor)
	 */
	public boolean isDataFlavorSupported(DataFlavor arg0)
	{
		if(flavor.equals(arg0))
		{
			return true;
		}
		return false;
	}

}
